<template>
	<view class="content">
		<view class="login-bg">
			<image src="../../static/logbg.png" mode="aspectFill" style="width: 222rpx;height: 255rpx;"></image>
			<view class="login-text">
				<view class="title">欢迎你！</view>
				<view class="">来到空气动力系统服务管家</view>
			</view>
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<view style="width: 688rpx;margin-top: 100rpx;">
			<u-button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" type="primary" text="手机号快捷登录"
				shape="circle" customStyle="height: 90rpx;"></u-button>
		</view>
		<!-- #endif -->
		<!-- #ifdef MP-ALIPAY -->
		<view style="width: 688rpx;margin-top: 100rpx;">
			<button open-type="getAuthorize" scope="phoneNumber" @getAuthorize="getPhoneNumber" @error="handleAuthError"
				class="zfb-but">
				手机号快捷登录
			</button>
		</view>
		<!-- #endif -->
		<view style="width: 688rpx;margin-top: 37rpx;" @click="phoneLogin">
			<u-button type="primary" text="暂不登录" plain shape="circle" customStyle="height: 90rpx;"></u-button>
		</view>
		<!-- <view style="width: 688rpx;margin-top: 30rpx;font-size: 25rpx;color: #3c9cff;margin-left: 13rpx;">
			<u-checkbox-group v-model="checkboxValue">
				<u-checkbox shape="circle" activeColor="#3c9cff" size="27rpx" iconSize="20rpx" labelSize="25rpx"
					labelColor="25rpx" label="我已阅读并同意" name="1"></u-checkbox>
				<text @click="goPath('/pages/user/fw')">“用户服务协议”</text>
				<text>及</text>
				<text @click="goPath('/pages/user/ys')">“隐私声明”</text>
			</u-checkbox-group>
		</view> -->
	</view>
</template>

<script>
	import {
		wxloginApi,
		aliLoginApi,
		zfbLogin
	} from "@/api"
	export default {
		data() {
			return {
				checkboxValue: [],
				code: ""
			}
		},
		onLoad() {
			this.getCode()
		},
		methods: {
			goPath(url) {
				uni.navigateTo({
					url: url
				})
			},
			// #ifdef MP-WEIXIN
			async getCode() {
				const res = await uni.login()
				console.log(123, res);
				this.code = res.code
			},
			// #endif
			// #ifdef MP-ALIPAY
			async getCode() {
				const {
					authCode
				} = await uni.login({
					scopes: 'auth_user'
				})
				this.code = authCode
				console.log(123, this.code);
			},
			// #endif

			// #ifdef MP-WEIXIN
			async getPhoneNumber(e) {
				// if (!this.checkboxValue.length) {
				// 	uni.showToast({
				// 		title: '请同意协议',
				// 		icon: 'none'
				// 	})
				// 	return
				// }

				if (e.detail.errMsg == 'getPhoneNumber:ok') {
					uni.showLoading({
						title: '登入中...',
						mask: true
					})
					try {
						// const {
						// 	code
						// } = await uni.login()
						// console.log(code);
						const res = await wxloginApi({
							code: this.code,
							iv: e.detail.iv,
							encryptedData: e.detail.encryptedData
						})
						uni.setStorageSync('token', res.data.token)
						uni.hideLoading()
						const deviceNo = uni.getStorageSync('deviceNo') || ''
						console.log(deviceNo);
						uni.reLaunch({
							url: '/pages/index/index?scene=' + deviceNo
						})
					} catch (e) {
						uni.hideLoading()
					}

				}
			},
			// #endif
			// #ifdef MP-ALIPAY
			getPhoneNumber() {
				console.log(6666);
				my.getPhoneNumber({
					success: async (res) => {
						const resb = await zfbLogin({
							code: this.code
						})
						let encryptedData = JSON.parse(res.response);
						const resa = await aliLoginApi({
							encryptedData: encryptedData.response,
							iv: resb.data.aliUserInfo.userId
						})

						uni.setStorageSync('token', resa.data.token)
						uni.hideLoading()
						const deviceNo = uni.getStorageSync('deviceNo') || ''
						console.log(deviceNo);
						uni.reLaunch({
							url: '/pages/index/index?scene=' + deviceNo
						})
					},
					fail: (res) => {
						console.log(res);
					},
				});
			},

			handleAuthError(e) {
				console.log(e);
			},
			// #endif
			phoneLogin() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100vw;
		height: 100vh;
		background: #FFFFFF;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 300rpx;

		/deep/ .u-button__text {
			font-weight: 500 !important;
			font-size: 30rpx !important;
			letter-spacing: 5rpx !important;

		}
	}

	.login-bg {
		width: 100vw;
		display: flex;
		align-items: center;
	}

	.login-text {
		font-size: 31rpx;
		color: #666666;
		margin-top: 6rpx;
	}

	.title {
		font-weight: bold;
		font-size: 35rpx;
		color: #000;

	}

	.zfb-but {
		border-radius: 100px;
		color: #fff;
		background-color: #3c9cff;
		border-color: #3c9cff;
		border-width: 1px;
		border-style: solid;
	}
</style>